package org.joseph.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@Slf4j
@RestController
@RequestMapping("/file")
public class FileuploadController {

    @RequestMapping("/upload")
    public Object upload(@RequestParam("file")MultipartFile uploadFile ) throws IOException{
        log.debug("upload filename "+uploadFile.getOriginalFilename());
        log.debug("upload file size "+ uploadFile.getSize());
        File file = new File("d:/upload/"+uploadFile.getResource().getFilename());
        if(!file.getParentFile().exists())
            file.getParentFile().mkdirs();
        try {
            uploadFile.transferTo(file);
            return "true";
        }catch (Exception e) {
            e.printStackTrace();
            return "false";
        }
    }
}
